﻿using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using System.Reflection;

namespace Moven.Droid
{
	public static class ViewExtensions
	{
		private static readonly Type _platformType = Type.GetType("Xamarin.Forms.Platform.Android.Platform, Xamarin.Forms.Platform.Android", true);
		private static BindableProperty _rendererProperty;

		public static BindableProperty RendererProperty
		{
			get
			{
				_rendererProperty = (BindableProperty)_platformType.GetField("RendererProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
					.GetValue(null);

				return _rendererProperty;
			}
		}

		public static IVisualElementRenderer GetRenderer(this VisualElement bindableObject)
		{
			return Platform.GetRenderer(bindableObject);
		}

		public static Android.Views.View GetNativeView(this VisualElement bindableObject)
		{
			var renderer = bindableObject.GetRenderer();
			return renderer?.ViewGroup;
		}

		public static void SetRenderer(this VisualElement bindableObject, IVisualElementRenderer renderer)
		{
			//			var value = bindableObject.GetValue (RendererProperty);
			bindableObject.SetValue(RendererProperty, renderer);
		}

		public static Point GetNativeScreenPosition(this VisualElement bindableObject)
		{
			var view = bindableObject.GetNativeView();
			var point = Point.Zero;
			if (view != null)
			{
				int[] location = new int[2];
				view.GetLocationOnScreen(location);
				point = new Point(location[0], location[1]);
			}
			return point;
		}

		/// <summary>
		/// Gets the or create renderer.
		/// </summary>
		/// <returns>The or create renderer.</returns>
		/// <param name="source">Source.</param>
		public static IVisualElementRenderer GetOrCreateRenderer(this VisualElement source)
		{
			var renderer = source.GetRenderer();
			if (renderer == null)
			{
				renderer = RendererFactory.GetRenderer(source);
				source.SetRenderer(renderer);
				renderer = source.GetRenderer();
			}
			return renderer;
		}
	}
}